<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>利用indexof查找元素出现的次数</title>
</head>
<body>
<div id="newArr"></div>
</body>
<script type="text/javascript">
    /**
     * @Description:
     * @author 朱志文
     * @date 2021/9/24
     */
    var arr = ['a', 'b', 'd', 'd', 'c', 'd', 'd'];//数组
    var newArr = new Array();
    let index = arr.indexOf('d');
    while (index != -1) { //若没有找到d 则返回1 找到d 范围所在位置的下标
        newArr.push(index); //新下标添加到新数组
        index = arr.indexOf('d', index + 1)
    }
    // console.log(newArr) //测试结果是否正确
    /*打印结果*/
    document.write(`<div>arr中元素d出现的次数:` + newArr.length + `;</div>`)
    document.write(`对应的所有索引下标:`)
    for (let i = 0; i < newArr.length; i++) {
        document.write(newArr[i] + ',')
    }

</script>
</html>